home *** CD-ROM | disk | FTP | other *** search
/ mail.altrad.com / 2015.02.mail.altrad.com.tar / mail.altrad.com / TEST / office deutch / INFOPATH.NL-NL / INFLR.CAB / TCARDSMP.XSN_1043 / script.js < prev    next >
Text File  |  2006-11-12  |  24KB  |  757 lines

  1. /*
  2.  * This file contains functions for data validation and form-level events.
  3.  * Because the functions are referenced in the form definition (.xsf) file, 
  4.  * it is recommended that you do not modify the name of the function,
  5.  * or the name and number of arguments.
  6.  *
  7. */
  8.  
  9. // The following line is created by Microsoft Office InfoPath to define the
  10. // prefixes for all the known namespaces in the main XML data file.
  11. // Any modification to the form files made outside of InfoPath
  12. // will not be autmatically updated.
  13. //<namespacesDefinition>
  14. XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD" xmlns:tc="http://schemas.microsoft.com/office/infopath/2003/sample/TimeCard"');
  15. //</namespacesDefinition>
  16.  
  17. /*------------------------------------------------------------------------------
  18.     This function handler is generated and managed automatically.
  19.     Do not rename the function or alter its parameter list.
  20. ------------------------------------------------------------------------------*/
  21. function XDocument::OnLoad(oEvent)
  22. {
  23.     // Avoid DOM updates when the document has been digitally signed.
  24.     if (XDocument.IsSigned)
  25.         return;
  26.  
  27.     initializeNodeValue("/tc:timeCard/tc:week/tc:weekOf", getDateString());
  28. }
  29.  
  30. /*------------------------------------------------------------------------------
  31.     This function handler is generated and managed automatically.
  32.     Do not rename the function or alter its parameter list.
  33. ------------------------------------------------------------------------------*/
  34. function msoxd__currency::OnAfterChange(oEvent)
  35. {
  36.  
  37.     if (oEvent.IsUndoRedo)
  38.         return;
  39.  
  40.     // Avoid DOM updates when the document has been digitally signed.
  41.     if (XDocument.IsSigned)
  42.         return;
  43.  
  44.     updateCurrency(oEvent.Site.parentNode, "tc:");
  45.     return;
  46. }
  47.  
  48.  
  49. /*------------------------------------------------------------------------------
  50.     This function handler is generated and managed automatically.
  51.     Do not rename the function or alter its parameter list.
  52. ------------------------------------------------------------------------------*/
  53. function msoxd__hours::OnAfterChange(oEvent)
  54. {
  55.     // Avoid side effects when DOM is read only
  56.     if (oEvent.IsUndoRedo)
  57.         return;
  58.  
  59.     // Avoid DOM updates when the document has been digitally signed.
  60.     if (XDocument.IsSigned)
  61.         return;
  62.  
  63.     updateTotals();
  64. }
  65.  
  66. /*==============================================================================
  67.     Private functions
  68. ==============================================================================*/
  69.  
  70. function updateTotals()
  71. {
  72.     var nTotalHours = 0;
  73.     var nTotalRegularHours = 0;
  74.     var nTotalOvertimeHours = 0;
  75.     var nTotalMiscHours = 0;
  76.     var xmlDays = XDocument.DOM.selectNodes("/tc:timeCard/tc:week/*[starts-with(name(),'tc:day')]");
  77.     var xmlDay;
  78.     var xmlNode;
  79.  
  80.     while (xmlDay = xmlDays.nextNode())
  81.         {
  82.         var xmlSubShifts = xmlDay.selectNodes("*[starts-with(name(),'tc:subShift')]");
  83.         var xmlSubShift;
  84.         var nDayTotal = 0;
  85.  
  86.         // calculate totals
  87.         while (xmlSubShift = xmlSubShifts.nextNode())
  88.             {
  89.             var xmlTypedHours = xmlSubShift.firstChild;
  90.  
  91.             while (xmlTypedHours)
  92.                 {
  93.                 var xmlHours = xmlTypedHours.selectSingleNode("tc:hours");
  94.                 var nHours = (isInvalidOrEmpty(xmlHours, "SCHEMA_VALIDATION", "SYSTEM_GENERATED") ? 0 : getNodeTypedValue(xmlHours, null, true));
  95.  
  96.                 switch (xmlTypedHours.nodeName)
  97.                     {
  98.                 case "tc:regularHours":
  99.                     nTotalRegularHours += nHours;
  100.                     break;
  101.     
  102.                 case "tc:overtimeHours":
  103.                     nTotalOvertimeHours += nHours;
  104.                     break;
  105.     
  106.                 default:
  107.                     nTotalMiscHours += nHours;
  108.                     break;
  109.                     }
  110.  
  111.                 nDayTotal += nHours;
  112.                 nTotalHours += nHours;
  113.  
  114.                 xmlTypedHours = xmlTypedHours.nextSibling;
  115.                 }
  116.             }
  117.  
  118.         // update day hours
  119.         xmlNode = xmlDay.selectSingleNode("tc:dayHours");
  120.         if (getNodeTypedValue(xmlNode, null, true) != nDayTotal)
  121.             setRoundedValue(xmlNode, nDayTotal);
  122.         }
  123.  
  124.     // update totals
  125.     xmlNode = XDocument.DOM.selectSingleNode("/tc:timeCard/tc:week/tc:totalHours");
  126.     if (getNodeTypedValue(xmlNode, null, true) != nTotalHours)
  127.         setRoundedValue(xmlNode, nTotalHours);
  128.  
  129.     xmlNode = XDocument.DOM.selectSingleNode("/tc:timeCard/tc:week/tc:totalRegularHours");
  130.     if (getNodeTypedValue(xmlNode, null, true) != nTotalRegularHours)
  131.         setRoundedValue(xmlNode, nTotalRegularHours);
  132.  
  133.     xmlNode = XDocument.DOM.selectSingleNode("/tc:timeCard/tc:week/tc:totalOvertimeHours");
  134.     if (getNodeTypedValue(xmlNode, null, true) != nTotalOvertimeHours)
  135.         setRoundedValue(xmlNode, nTotalOvertimeHours);
  136.  
  137.     xmlNode = XDocument.DOM.selectSingleNode("/tc:timeCard/tc:week/tc:totalMiscHours");
  138.     if (getNodeTypedValue(xmlNode, null, true) != nTotalMiscHours)
  139.         setRoundedValue(xmlNode, nTotalMiscHours);
  140. }
  141.  
  142. /*------------------------------------------------------------------------------
  143.  
  144.     Common form template functions section - the following code is common to all out-of-box form templates.
  145.  
  146. ------------------------------------------------------------------------------*/
  147.  
  148. /* =============================================================================
  149.     Currency handling
  150. ============================================================================= */
  151.  
  152. /*------------------------------------------------------------------------------
  153.     updateCurrency()
  154. ------------------------------------------------------------------------------*/
  155. function updateCurrency(xmlNode, strPrefix)
  156. {
  157.     var xmlName = xmlNode.selectSingleNode(strPrefix + 'name');
  158.     var xmlSymbol = xmlNode.selectSingleNode(strPrefix + 'symbol');
  159.  
  160.     var strName = xmlName.text;
  161.     var strSymbol = lookupCurrencySymbol(strName);
  162.  
  163.     if (strSymbol)
  164.     {
  165.         setNodeValue(xmlSymbol, '(' + strSymbol + ') ');
  166.     }
  167.     else
  168.     {
  169.         setNodeValue(xmlName, '');
  170.         setNodeValue(xmlSymbol, '');
  171.     }
  172. }
  173.  
  174. /*------------------------------------------------------------------------------
  175.     lookupCurrencySymbol()
  176. ------------------------------------------------------------------------------*/
  177. function lookupCurrencySymbol(strName)
  178. {
  179.     var xmlCurDom = XDocument.GetDOM("currencies");
  180.     var xmlCurrency = xmlCurDom.selectSingleNode("/currencies/currency[@name = '" + strName + "']");
  181.  
  182.     if (xmlCurrency)
  183.         return xmlCurrency.getAttribute("symbol");
  184. }
  185.  
  186. /* =============================================================================
  187.     Date/Time handling
  188. ============================================================================= */
  189.  
  190. /*------------------------------------------------------------------------------
  191.     getDateString()
  192. ------------------------------------------------------------------------------*/
  193. function getDateString(oDate)
  194. {
  195.     // Use today as default value.
  196.     if (oDate == null)
  197.         oDate = new Date();
  198.  
  199.     var m = oDate.getMonth() + 1;
  200.     var d = oDate.getDate();
  201.  
  202.     if (m < 10)
  203.         m = "0" + m;
  204.  
  205.     if (d < 10)
  206.         d = "0" + d;
  207.  
  208.     // ISO 8601 date (YYYY-MM-DD).
  209.     return oDate.getFullYear() + "-" + m + "-" + d;
  210. }
  211.  
  212. /*------------------------------------------------------------------------------
  213.     getTimeString()
  214. ------------------------------------------------------------------------------*/
  215. function getTimeString(oTime)
  216. {
  217.     // Use now as default value.
  218.     if (oTime == null)
  219.         oTime = new Date();
  220.  
  221.     var h = oTime.getHours();
  222.     var m = oTime.getMinutes();
  223.     var s = oTime.getSeconds();
  224.     var ms = oTime.getMilliseconds();
  225.  
  226.     if (h < 10)
  227.         h = "0" + h;
  228.  
  229.     if (m < 10)
  230.         m = "0" + m;
  231.  
  232.     if (s < 10)
  233.         s = "0" + s;
  234.  
  235.     if (ms < 100)
  236.         ms = "0" + (ms < 10 ? "0" : "") + ms;
  237.  
  238.     // ISO 8601 time (HH:MM:SS.SSS).
  239.     return h + ":" + m + ":" + s + "." + ms;
  240. }
  241. /* =============================================================================
  242.     Language conversion operations    
  243. ============================================================================= */
  244.  
  245. /*------------------------------------------------------------------------------
  246.     convertXMLNumberToJScript()
  247. ------------------------------------------------------------------------------*/
  248. function convertXMLNumberToJScript(value)
  249. {
  250.     var retVal;
  251.  
  252.     switch (value)
  253.     {
  254.         case "-INF":
  255.             retVal = Number.NEGATIVE_INFINITY;
  256.             break;
  257.  
  258.         case "INF":
  259.             retVal = Number.POSITIVE_INFINITY;
  260.             break;
  261.  
  262.         case "NaN":
  263.             retVal = NaN;
  264.             break;
  265.  
  266.         default:
  267.             //If no prefix of its argument can be parsed as a
  268.             //float, parseFloat will return NaN. Otherwise, it
  269.             //will return the value of the longest float prefix
  270.             //it can find in its argument.
  271.             retVal = parseFloat(value);
  272.     }
  273.  
  274.     return retVal;
  275. }
  276.  
  277. /*------------------------------------------------------------------------------
  278.     convertJScriptNumberToXML()
  279. ------------------------------------------------------------------------------*/
  280. function convertJScriptNumberToXML(value)
  281. {
  282.     var retVal;
  283.  
  284.     switch (value)
  285.     {
  286.         case Number.NEGATIVE_INFINITY:
  287.             retVal = "-INF";
  288.             break;
  289.  
  290.         case Number.POSITIVE_INFINITY:
  291.             retVal = "INF";
  292.             break;
  293.  
  294.         //NaN == NaN returns false. Any other value is equal to itself,
  295.         //which is why the case below differentiates between NaN and
  296.         //any other valid value
  297.         case value:
  298.             retVal = value;
  299.             break;
  300.  
  301.         default:
  302.             retVal = "NaN";
  303.     }
  304.  
  305.     return retVal;
  306. }
  307.  
  308. /* =============================================================================
  309.     Node value operations
  310. ============================================================================= */
  311.  
  312. /*------------------------------------------------------------------------------
  313.     isInvalidOrEmpty()
  314. ------------------------------------------------------------------------------*/
  315. function isInvalidOrEmpty(xmlNode)
  316. {
  317.     // If there is no value, ignore it.
  318.     if (!xmlNode || !xmlNode.text)
  319.         return true;
  320.  
  321.     // The caller can pass additional error types as optional arguments.
  322.     var aErrorTypes = new Array;
  323.     if (arguments.length > 1)
  324.     {
  325.         for (var i=1; i<arguments.length; i++)
  326.             aErrorTypes.push(arguments[i]);
  327.     }
  328.     else
  329.     {
  330.         aErrorTypes.push("SCHEMA_VALIDATION");
  331.     }
  332.  
  333.     // If there is a validation error related to this node,
  334.     // then the node is invalid.
  335.     for (var i=0; i<XDocument.Errors.Count; i++)
  336.     {
  337.         var oError = XDocument.Errors(i);
  338.  
  339.         if (xmlNode == oError.Node)
  340.         {
  341.             for (var j in aErrorTypes)
  342.             {
  343.                 if (oError.Type == aErrorTypes[j])
  344.                     return true;
  345.             }
  346.         }
  347.     }
  348.  
  349.     // Is valid (no error was found).
  350.     return false;
  351. }
  352.  
  353. /*------------------------------------------------------------------------------
  354.     getNodeValue()
  355. ------------------------------------------------------------------------------*/
  356. function getNodeValue(xpath, defaultValue)
  357. {
  358.     var xmlNode = getNode(xpath);
  359.  
  360.     if (isInvalidOrEmpty(xmlNode))
  361.         return (arguments.length > 1) ? defaultValue : "";
  362.     else
  363.         return xmlNode.text;
  364. }
  365.  
  366. /*------------------------------------------------------------------------------
  367.     getNodeTypedValue()    - Use instead of getNodeValue() if you want
  368.         to parse the value by MSXML according to data type (from schema).
  369.         
  370.     NOTE: this is not really useful for dates in Jscript.
  371. ------------------------------------------------------------------------------*/
  372. function getNodeTypedValue(xpath, defaultValue, fNumber)
  373. {
  374.     var xmlNode = getNode(xpath);
  375.  
  376.     if (isInvalidOrEmpty(xmlNode))
  377.         return (arguments.length > 1) ? defaultValue : "";
  378.     
  379.     if (fNumber)
  380.         return convertXMLNumberToJScript(xmlNode.nodeTypedValue);
  381.     else
  382.         return xmlNode.nodeTypedValue;
  383. }
  384.  
  385. /*------------------------------------------------------------------------------
  386.     setNodeValue()
  387. ------------------------------------------------------------------------------*/
  388. function setNodeValue(xpath, value)
  389. {
  390.     var xmlNode = getNode(xpath);
  391.  
  392.     if (!xmlNode)
  393.         return;
  394.  
  395.     // The xsi:nil needs to be removed before we set the value.
  396.     if (value != "" && xmlNode.getAttribute("xsi:nil"))
  397.         xmlNode.removeAttribute("xsi:nil");
  398.  
  399.     // Setting the value would mark the document as dirty.
  400.     // Let's do that if the value has really changed.
  401.     if (xmlNode.text != value)
  402.         xmlNode.text = value;
  403. }
  404.  
  405. /*------------------------------------------------------------------------------
  406.     setNodeTypedValue() - Use instead of setNodeValue() if you want
  407.         to format the value by MSXML according to data type (from schema).
  408.         
  409.         Use this when setting floating point and decimal values. These
  410.         values are formatted differently in some locales and will cause schema
  411.         validation errors when set using setNodeValue or a node's text property.
  412.         
  413.     NOTE: this is not really useful for dates in Jscript.
  414. ------------------------------------------------------------------------------*/
  415. function setNodeTypedValue(xpath, value)
  416. {
  417.     var xmlNode = getNode(xpath);
  418.     
  419.     if (!xmlNode)
  420.         return;
  421.  
  422.     // The xsi:nil needs to be removed before we set the value.
  423.     if (value != "" && xmlNode.getAttribute("xsi:nil"))
  424.         xmlNode.removeAttribute("xsi:nil");
  425.  
  426.     var convertedValue = convertJScriptNumberToXML(value);
  427.  
  428.     // Setting the value would mark the document as dirty.
  429.     // Let's do that if the value has really changed.
  430.     if (xmlNode.nodeTypedValue != convertedValue)
  431.         xmlNode.nodeTypedValue = convertedValue;
  432. }
  433.  
  434. /*------------------------------------------------------------------------------
  435.     setRoundedValue() - Rounds the value to the default number of decimal
  436.     places and calls setNodeTypedValue() with the result.
  437. ------------------------------------------------------------------------------*/
  438. function setRoundedValue(xpath, value)
  439. {
  440.     var xmlNode = getNode(xpath);
  441.  
  442.     if (!xmlNode)
  443.         return;
  444.  
  445.     var roundedValue = roundFloat(value, 3);
  446.     setNodeTypedValue(xpath, roundedValue);
  447. }
  448.  
  449. /*------------------------------------------------------------------------------
  450.     setNil() - Empty a nillable element.
  451. ------------------------------------------------------------------------------*/
  452. function setNil(xpath)
  453. {
  454.     var xmlNode = getNode(xpath);
  455.  
  456.     if (!xmlNode || xmlNode.text === "")
  457.         return;
  458.  
  459.     // Create xsi:nil attribute with the proper namespace.
  460.     var xmlNil = xmlNode.ownerDocument.createNode(2, "xsi:nil", "http://www.w3.org/2001/XMLSchema-instance");
  461.     xmlNil.text = "true";
  462.  
  463.     // The order is important.
  464.     xmlNode.text = "";
  465.     xmlNode.setAttributeNode(xmlNil);
  466. }
  467.  
  468. /*------------------------------------------------------------------------------
  469.     roundFloat() - Rounds the value to the specified number of decimal places.
  470. ------------------------------------------------------------------------------*/
  471. function roundFloat(value, decimalPlaces)
  472. {
  473.     if (value < -1E15 || 1E15 < value)
  474.     {
  475.         return value;
  476.     }
  477.     else
  478.     {
  479.         var nPowerToRound = Math.pow(10, decimalPlaces);
  480.         return Math.round(value*nPowerToRound)/nPowerToRound;
  481.     }
  482. }
  483.  
  484. /*------------------------------------------------------------------------------
  485.     normalizeSource() - Ensures that we always get a node of type NODE_ELEMENT
  486.         or NODE_ATTRIBUTE.
  487. ------------------------------------------------------------------------------*/
  488. function normalizeSource(oEvent)
  489. {
  490.     // Return the node's parent if the node is of type NODE_TEXT and its parent
  491.     // is of type NODE_ELEMENT
  492.     if (oEvent.Parent.nodeType == 1 && oEvent.Source.nodeType == 3)
  493.         return oEvent.Parent;
  494.         
  495.     // Return the node if it is of type NODE_ELEMENT or NODE_ATTRIBUTE;
  496.     if (oEvent.Source.nodeType == 1 || oEvent.Source.nodeType == 2)
  497.         return oEvent.Source;
  498. }
  499.  
  500. /*------------------------------------------------------------------------------
  501.     normalizeParent() - Ensures that we always get the parent of a node of 
  502.         type NODE_ELEMENT or NODE_ATTRIBUTE.
  503. ------------------------------------------------------------------------------*/
  504. function normalizeParent(oEvent)
  505. {
  506.     // Return the node if it is of type NODE_ELEMENT or NODE_ATTRIBUTE;
  507.     if (oEvent.Source.nodeType == 1 || oEvent.Source.nodeType == 2)
  508.         return oEvent.Parent;
  509.         
  510.     // Return the node 's parent if the node is of type NODE_TEXT and its parent
  511.     // is of type NODE_ELEMENT
  512.     if (oEvent.Parent.nodeType == 1 && oEvent.Source.nodeType == 3)
  513.         return oEvent.Parent.parentNode;
  514. }
  515.  
  516. /*------------------------------------------------------------------------------
  517.     initializeNodeValue()
  518. ------------------------------------------------------------------------------*/
  519. function initializeNodeValue(xpath, strValue)
  520. {
  521.     var xmlNode = getNode(xpath);
  522.  
  523.     // Set the node value *ONLY* if the node is empty.
  524.     if (xmlNode.text == "")
  525.         setNodeValue(xmlNode, strValue);
  526. }
  527.  
  528. /* =============================================================================
  529.     Simple nodelist operations
  530. ============================================================================= */
  531.  
  532. /*------------------------------------------------------------------------------
  533.     getNode()
  534. ------------------------------------------------------------------------------*/
  535. function getNode(xpath)
  536. {
  537.     // Both XML node and absolute XPath are allowed.
  538.     if (typeof(xpath) == "string")
  539.         return XDocument.DOM.selectSingleNode(xpath);
  540.     else
  541.         return xpath;
  542. }
  543.  
  544. /*------------------------------------------------------------------------------
  545.     getNodeList()
  546. ------------------------------------------------------------------------------*/
  547. function getNodeList(xpath)
  548. {
  549.     // Both XML node and absolute XPath are allowed.
  550.     if (typeof(xpath) == "string")
  551.         return XDocument.DOM.selectNodes(xpath);
  552.     else
  553.         return xpath;
  554. }
  555.  
  556. /*------------------------------------------------------------------------------
  557.     count()
  558. ------------------------------------------------------------------------------*/
  559. function count(xpath)
  560. {
  561.     var xmlNodeList = getNodeList(xpath);
  562.  
  563.     if (xmlNodeList)
  564.         return xmlNodeList.length;
  565.     else
  566.         return -1;
  567. }
  568.  
  569. /* =============================================================================
  570.     Complex nodelist operations
  571. ============================================================================= */
  572.  
  573. /*------------------------------------------------------------------------------
  574.     applyAction() - Iterate the whole nodelist and apply a given action
  575.         to each node in the nodelist. You can implement your own actions.
  576.  
  577.     Example:
  578.         var oAction = new Object();
  579.         oAction.sum = 0;
  580.         oAction.apply = new Function("xmlNode", "this.sum += xmlNode.nodeTypedValue");
  581. ------------------------------------------------------------------------------*/
  582. function applyAction(xpath, oAction)
  583. {
  584.     var xmlNodeList = getNodeList(xpath);
  585.     var xmlNode;
  586.  
  587.     while (xmlNode = xmlNodeList.nextNode())
  588.         oAction.apply(xmlNode);
  589.  
  590.     xmlNodeList.reset();
  591. }
  592.  
  593. /*------------------------------------------------------------------------------
  594.     sum()
  595. ------------------------------------------------------------------------------*/
  596. function sum(xpath)
  597. {
  598.     var oAction = new Object;
  599.     oAction.nResult = 0;
  600.     oAction.apply = new Function("xmlNode", "this.nResult += getNodeTypedValue(xmlNode, 0, true)");
  601.  
  602.     applyAction(xpath, oAction);
  603.     return oAction.nResult;
  604. }
  605.  
  606. /*------------------------------------------------------------------------------
  607.     anyInvalidOrEmpty() - Searches a nodelist to see if there are any invalid 
  608.         or empty nodes.
  609. ------------------------------------------------------------------------------*/
  610. function anyInvalidOrEmpty(xpath)
  611. {
  612.     var oAction = new Object;
  613.     oAction.fInvalid = false;
  614.     oAction.apply = new Function("xmlNode", "this.fInvalid |= isInvalidOrEmpty(xmlNode)");
  615.     
  616.     applyAction(xpath, oAction);
  617.     return oAction.fInvalid;
  618. }
  619.  
  620. /*------------------------------------------------------------------------------
  621.     reindex()
  622. ------------------------------------------------------------------------------*/
  623. function reindex(xpath, iStart)
  624. {
  625.     if (iStart == null)
  626.         iStart = 1;
  627.  
  628.     var oAction = new Object;
  629.     oAction.i = iStart;
  630.     oAction.apply = new Function("xmlNode", "setNodeTypedValue(xmlNode, this.i++)");
  631.  
  632.     return applyAction(xpath, oAction);
  633. }
  634.  
  635. /*------------------------------------------------------------------------------
  636.     average()
  637. ------------------------------------------------------------------------------*/
  638. function average(xpath, xpathWeights)
  639. {
  640.     var oAction = new Object;
  641.     oAction.nSum = 0;
  642.     oAction.cNodes = 0;
  643.  
  644.     if (!xpathWeights)
  645.     {
  646.         oAction.apply = actionSimpleAverage;
  647.         oAction.result = resultSimpleAverage;
  648.     }
  649.     else
  650.     {
  651.         oAction.nWeights = 0;
  652.         oAction.xmlWeights = getNodeList(xpathWeights);
  653.         oAction.apply = actionWeightedAverage;
  654.         oAction.result = resultWeightedAverage;
  655.     }
  656.  
  657.     applyAction(xpath, oAction);
  658.     return oAction.result();
  659. }
  660.  
  661. /*------------------------------------------------------------------------------
  662.     actionSimpleAverage()
  663. ------------------------------------------------------------------------------*/
  664. function actionSimpleAverage(xmlNode)
  665. {
  666.     if (!isInvalidOrEmpty(xmlNode))
  667.     {
  668.         this.nSum += getNodeTypedValue(xmlNode, 0, true);
  669.         this.cNodes++;
  670.     }
  671. }
  672.  
  673. /*------------------------------------------------------------------------------
  674.     resultSimpleAverage()
  675. ------------------------------------------------------------------------------*/
  676. function resultSimpleAverage()
  677. {
  678.     if (this.cNodes)
  679.         return this.nSum / this.cNodes;
  680.     else
  681.         return 0;
  682. }
  683.  
  684. /*------------------------------------------------------------------------------
  685.     actionWeightedAverage()
  686. ------------------------------------------------------------------------------*/
  687. function actionWeightedAverage(xmlNode)
  688. {
  689.     // There should be one weight for each node. Even if the node is invalid,
  690.     // we have to advance the iterator of weights to keep them in sync.
  691.     var xmlWeight = this.xmlWeights.nextNode();
  692.  
  693.     if (!isInvalidOrEmpty(xmlNode) && !isInvalidOrEmpty(xmlWeight))
  694.     {
  695.         var nValue = getNodeTypedValue(xmlNode, 0, true);
  696.         var nWeight = getNodeTypedValue(xmlWeight, 1, true);
  697.  
  698.         this.nSum += nValue * nWeight;
  699.         this.nWeights += nWeight;
  700.         this.cNodes++;
  701.     }
  702. }
  703.  
  704. /*------------------------------------------------------------------------------
  705.     resultWeightedAverage()
  706. ------------------------------------------------------------------------------*/
  707. function resultWeightedAverage()
  708. {
  709.     this.xmlWeights.reset();
  710.  
  711.     if (this.cNodes)
  712.     {
  713.         if (this.nWeights > 0)
  714.             return this.nSum / this.nWeights;
  715.         else
  716.             return this.nSum
  717.     }
  718.     else
  719.     {
  720.         return 0;
  721.     }
  722. }
  723.  
  724. /*------------------------------------------------------------------------------
  725.     sort()
  726. ------------------------------------------------------------------------------*/
  727. function sort(xpathParent, xpathChildren, fnCompare)
  728. {
  729.     // Retrieve the parent node; all the children will be sorted.
  730.     var xmlOrigParent = getNode(xpathParent)
  731.     var xmlItems = xmlOrigParent.selectNodes(xpathChildren);
  732.  
  733.     if (1 < count(xmlItems))
  734.     {
  735.         // Store the (pointers to) items in an array for faster access.
  736.         rgItems = new Array();
  737.         
  738.         while (xmlItem = xmlItems.nextNode())
  739.             rgItems.push(xmlItem);
  740.             
  741.         // Sort the array (the XML does not change).
  742.         rgItems.sort(fnCompare);
  743.  
  744.         // Now that the array is sorted the DOM should be updated.
  745.         var xmlSortParent = xmlOrigParent.cloneNode(true);
  746.         var xmlClones = xmlSortParent.selectNodes(xpathChildren);
  747.         xmlClones.removeAll();
  748.         
  749.         // Update the nodelist, each item is moved only once.
  750.         // (each change is causing several notifications to be fired.)
  751.         for (var i=0; i<rgItems.length; i++)
  752.             xmlSortParent.appendChild(rgItems[i].cloneNode(true));
  753.         
  754.         xmlOrigParent.parentNode.replaceChild(xmlSortParent, xmlOrigParent);
  755.     }
  756. }
  757.